package cn.itsource.org.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.org.domain.Department;
import cn.itsource.org.domain.Systemdictionarydetail;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;



    @Override
    public List<Systemdictionarydetail> detailById(Long id) {
        return departmentMapper.detailById(id);
    }



    /**
     * 简单写法 == 垃圾写法
     * @return
     */
//    @Override
//    public List<Department> tree() {
//        List<Department> result = new ArrayList<>();
//        // 查询所有数据
//        List<Department> allDepts = findAll();
//        // 遍历集合
//        for (Department dept : allDepts) {
//            if (null == dept.getParentId()){
//                // 顶级部门
//                result.add(dept);
//            }else {
//                // dept 一定是儿子
//                // 一定是某一个子级部门 去找它爹
//                for (Department parent : allDepts) {
//                    if (parent.getId().equals(dept.getParentId())){
//                        // 找到了dept它爹
//                        List<Department> children = parent.getChildren();
//                        children.add(dept);
//                        break;
//                    }
//                }
//            }
//        }
//        return result;
//    }
    /**
     * 高级技师玩法 这种玩法你们必须掌握
     */
    @Override
    public List<Department> tree() {
        List<Department> result = new ArrayList<>();
        // 查询所有数据
        List<Department> allDepts = findAll();
        // 将allDepts 转成map   key：id    value：dept对象
        Map<Long, Department> deptMap = allDepts.stream().collect(Collectors.toMap(Department::getId, dept->dept));
        // 替代写法
//        Map<Long, Department> deptMap = new HashMap<>();
//        for (Department dept : allDepts) {
//            deptMap.put(dept.getId(),dept);
//        }
        // 遍历集合
        for (Department dept : allDepts) {
            if (null == dept.getParentId()){
                // 顶级部门
                result.add(dept);
            }else {
                Department parent = deptMap.get(dept.getParentId());
                if (null != parent){
                    parent.getChildren().add(dept);
                }
                // dept 一定是儿子
                // 一定是某一个子级部门 去找它爹
//                for (Department parent : allDepts) {
//                    if (parent.getId().equals(dept.getParentId())){
//                        // 找到了dept它爹
//                        List<Department> children = parent.getChildren();
//                        children.add(dept);
//                        break;
//                    }
//                }
            }
        }
        return result;
    }
}
